package com.appointment.controller;


import com.appointment.dto.ProfessorDto;
import com.appointment.dto.ProfessorListRequestDto;
import com.appointment.dto.ProfessorSimpleListRequestDto;
import com.appointment.dto.TopListRequestDto;
import com.appointment.pojo.Appointment;
import com.appointment.pojo.Area;
import com.appointment.pojo.Professor;
import com.appointment.result.R;
import com.appointment.service.AppointmentService;
import com.appointment.service.AreaService;
import com.appointment.service.ProfessorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/professor")
public class ProfessorController {

    @Autowired
    private ProfessorService professorService;

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private AreaService areaService;

    @PostMapping("/user/getTopList")
    public R<Object> getTopList(@Valid @RequestBody TopListRequestDto topListRequestDto) {
        // 获取TOP数量
        int topNum = topListRequestDto.getTopNum();
        // 构造查询函数
        LambdaQueryWrapper<Professor> professorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        professorLambdaQueryWrapper.select(Professor::getId, Professor::getName, Professor::getAreaId, Professor::getPrice, Professor::getAvatarUrl);
        professorLambdaQueryWrapper.orderByDesc(Professor::getUpdateTime);
        professorLambdaQueryWrapper.last("limit " + topNum);
        List<Professor> professorlist = professorService.list(professorLambdaQueryWrapper);
        // 调整结构
        List<ProfessorDto> professorDtoList = professorlist.stream().map(professor -> {
            ProfessorDto professorDto = new ProfessorDto();
            BeanUtils.copyProperties(professor, professorDto);
            // 预约数 构造查询函数
            LambdaQueryWrapper<Appointment> appointmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            String professorId = professor.getId();
            appointmentLambdaQueryWrapper.select(Appointment::getId);
            appointmentLambdaQueryWrapper.eq(Appointment::getProfessorId, professorId);
            long count = appointmentService.count(appointmentLambdaQueryWrapper);
            professorDto.setAppointmentCount(count);
            // TODO 评价分数暂时随机数，后续根据实际情况修改
            professorDto.setEvaluationRating(99);
            // 领域 构造查询函数
            LambdaQueryWrapper<Area> areaQueryWrapper = new LambdaQueryWrapper<>();
            areaQueryWrapper.select(Area::getArea);
            areaQueryWrapper.eq(Area::getId, professor.getAreaId());
            Area area = areaService.getOne(areaQueryWrapper);
            if (area == null) {
                log.error("领域不存在:{}", professor.getAreaId());
                return professorDto;
            }
            professorDto.setArea(area.getArea());
            return professorDto;
        }).collect(Collectors.toList());
        return R.success(professorDtoList);
    }

    @PostMapping("/user/getListByName")
    public R<Object> getListByName(@Valid @RequestBody ProfessorListRequestDto professorListRequestDto) {
        // 获取搜索关键字
        String name = professorListRequestDto.getName();
        int pageNum = professorListRequestDto.getPageNum();
        int pageSize = professorListRequestDto.getPageSize();
        Page<Professor> professorPage = new Page<>(pageNum, pageSize);
        // 构造查询函数
        LambdaQueryWrapper<Professor> professorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        professorLambdaQueryWrapper.select(Professor::getId, Professor::getName, Professor::getAreaId, Professor::getPrice, Professor::getAvatarUrl);
        professorLambdaQueryWrapper.like(Professor::getName, name);
        professorLambdaQueryWrapper.orderByDesc(Professor::getUpdateTime);
        professorService.page(professorPage, professorLambdaQueryWrapper);
        List<Professor> records = professorPage.getRecords();
        Page<ProfessorDto> professorDtoPage = new Page<>(pageNum, pageSize);
        BeanUtils.copyProperties(professorPage, professorDtoPage, "records");
        // 调整结构
        List<ProfessorDto> professorDtoList = records.stream().map((professor) -> {
            ProfessorDto professorDto = new ProfessorDto();
            BeanUtils.copyProperties(professor, professorDto);
            // 预约数 构造查询函数
            LambdaQueryWrapper<Appointment> appointmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            String professorId = professor.getId();
            appointmentLambdaQueryWrapper.select(Appointment::getId);
            appointmentLambdaQueryWrapper.eq(Appointment::getProfessorId, professorId);
            long count = appointmentService.count(appointmentLambdaQueryWrapper);
            professorDto.setAppointmentCount(count);
            // TODO 评价分数暂时随机数，后续根据实际情况修改
            professorDto.setEvaluationRating(99);
            // 领域 构造查询函数
            LambdaQueryWrapper<Area> areaQueryWrapper = new LambdaQueryWrapper<>();
            areaQueryWrapper.select(Area::getArea);
            areaQueryWrapper.eq(Area::getId, professor.getAreaId());
            Area area = areaService.getOne(areaQueryWrapper);
            if (area == null) {
                log.error("领域不存在:{}", professor.getAreaId());
                return professorDto;
            }
            professorDto.setArea(area.getArea());
            return professorDto;
        }).collect(Collectors.toList());
        professorDtoPage.setRecords(professorDtoList);
        return R.success(professorDtoPage);
    }

    @PostMapping("/user/getSimpleListByName")
    public R<Object> getSimpleListByName(@Valid @RequestBody ProfessorSimpleListRequestDto professorSimpleListRequestDto) {
        // 获取搜索关键字
        String name = professorSimpleListRequestDto.getName();
        // 构造查询函数
        LambdaQueryWrapper<Professor> professorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        professorLambdaQueryWrapper.select(Professor::getId, Professor::getName, Professor::getAreaId);
        professorLambdaQueryWrapper.like(Professor::getName, name);
        professorLambdaQueryWrapper.orderByDesc(Professor::getUpdateTime);
        List<Professor> professorList = professorService.list(professorLambdaQueryWrapper);
        List<ProfessorDto> professorDtoList = professorList.stream().map((professor) -> {
            ProfessorDto professorDto = new ProfessorDto();
            BeanUtils.copyProperties(professor, professorDto);
            // 领域 构造查询函数
            LambdaQueryWrapper<Area> areaQueryWrapper = new LambdaQueryWrapper<>();
            areaQueryWrapper.select(Area::getArea);
            areaQueryWrapper.eq(Area::getId, professor.getAreaId());
            Area area = areaService.getOne(areaQueryWrapper);
            if (area == null) {
                log.error("领域不存在:{}", professor.getAreaId());
                return professorDto;
            }
            professorDto.setArea(area.getArea());
            return professorDto;
        }).collect(Collectors.toList());
        return R.success(professorDtoList);
    }
}
